/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-17 14:00:29  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.edu.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.edu.bean.*;
import com.joysuch.wwyt.edu.bean.EduTrainningPlanListBean.SearchEduTrainningPlan;
import com.joysuch.wwyt.edu.bean.EduTrainningPlanStatisticsVo.SearchTrainningPlan;
import com.joysuch.wwyt.edu.entity.EduTrainningPlan;
import com.joysuch.wwyt.edu.service.EduTrainningPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "教育-培训计划")
@RequestMapping("/edu/trainning-plan")
public class EduTrainningPlanController {

    private static final Logger logger = LoggerFactory.getLogger(EduTrainningPlanController.class);

    @Autowired
    private EduTrainningPlanService eduTrainningPlanService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    /**
     * 新增培训计划
     * @param entity
     * @return
     */
    @RequiresPermissions("edu:trainning:plan:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduTrainningPlanEditBean entity) {
        //获取培训部门id的集合
        if (entity.getDepartBeanList() != null) {
            List<Long> departIds = entity.getDepartBeanList().stream().map(EduDepartBean::getId).collect(Collectors.toList());
            entity.setTrainningDepartIds(departIds);
        }
        eduTrainningPlanService.add(entity);
        return ResultBean.success("");
    }

    /**
     * 修改培训计划
     * @param entity
     * @return
     */
    @RequiresPermissions("edu:trainning:plan:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduTrainningPlanEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        //获取培训部门的id的集合
        if (entity.getDepartBeanList() != null) {
            List<Long> departIds = entity.getDepartBeanList().stream().map(EduDepartBean::getId).collect(Collectors.toList());
            entity.setTrainningDepartIds(departIds);
        }
        eduTrainningPlanService.update(entity);
        return ResultBean.success("");
    }

    /**
     * 分页查询培训计划
     * @param page
     * @param condition
     * @return
     */
  //  @RequiresPermissions("edu:trainning:plan")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduTrainningPlan condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduTrainningPlanListBean> data = eduTrainningPlanService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

   // @RequiresPermissions("edu:trainning:plan")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduTrainningPlan condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduTrainningPlanListBean> data = eduTrainningPlanService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(EduTrainningPlan.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("edu:trainning:plan:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        eduTrainningPlanService.delete(id);
        return ResultBean.success("");
    }

  //  @RequiresPermissions("edu:trainning:plan")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduTrainningPlanService.getById(id));
    }

    /**
     * 我的培训计划（app）
     * @param page
     * @return
     */
    @GetMapping("myPage")
    @ApiOperation(value = "我的培训计划（app首页）")
    public Object myPage(Pageable page) {
        List<Map<String,Object>> data = eduTrainningPlanService.findByMyPage();
        return ResultBean.success(data);
    }

    /**
     * 我的培训计划（app全部）
     * @param page
     * @return
     */
    @GetMapping("myAllPage")
    @ApiOperation(value = "我的培训计划（app全部）")
    public ResultBean myAllPage(Pageable page,String status,Integer state) {
        return eduTrainningPlanService.findByMyAllPage(page,status,state);
    }

    /**
     * 培训计划详情（app）
     * @return
     */
    @GetMapping("detail")
    @ApiOperation(value = "培训计划详情（app）")
    public Object detail(@RequestParam("trainingPlanId") Long trainingPlanId) {
        EduTrainingPlanDetailVo detail = eduTrainningPlanService.findDetail(trainingPlanId);
        return ResultBean.success(detail);
    }

    /**
     * 培训计划统计（web）
     * @param page
     * @return
     */
    @GetMapping("statistics")
    @ApiOperation(value = "培训计划统计（web）")
    public Object statistics(Pageable page, SearchTrainningPlan condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduTrainningPlanStatisticsVo> data = eduTrainningPlanService.findStatisticsPage(page,condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("statistics/export")
    @ApiOperation(value = "培训计划统计-导出")
    public void export(SearchTrainningPlan condition, HttpServletRequest request, HttpServletResponse response) {
        eduTrainningPlanService.planExport(condition, request, response);
    }
}
